#include<iostream>
using namespace std;
#include<vector>
#include<map>
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
struct cmp
{
    bool operator()(const int x, const int y)
    {
        return x > y;
    }
};
class Solution
{
    priority_queue<int, vector<int>, cmp> q1;
public:
    int findSecondMinimumValue(TreeNode* root)
    {
        dfs(root);
        q1.pop();
        return q1.size() == 0 ? -1 : q1.top();
    }
    void dfs(TreeNode* root)
    {
        if (root == nullptr) return;
        if (q1.size() == 0 || root->val != q1.top())
        {
            q1.push(root->val);
        }
        dfs(root->left);
        dfs(root->right);
    }
};